package com.example.beat_x3.Fragments;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.example.beat_x3.Client.GetResult;
import com.example.beat_x3.R;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import Entity.Inf;

public class ChangingFragment extends MyFragment {
    EditText old_password, password, cpassword;
    TextView music_preference, prompt_password, prompt_cpassword;
    Button button1;
    RadioButton b1, b2;
    int sex = 1;
    String music_text = "", password_text = "", cpassword_text = "";

    boolean password_st = false, cpassword_st = false;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        @SuppressLint("InflateParams") View view = inflater.inflate(R.layout.fragment_changing, null);
        view.findViewById(R.id.change_backbutton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeFragment(Inf.centerFragment);
            }
        });
        old_password = view.findViewById(R.id.change_old_password);
        password = view.findViewById(R.id.change_password);
        cpassword = view.findViewById(R.id.change_ConfirmPassword);
        music_preference = view.findViewById(R.id.change_music_preference);
        prompt_password = view.findViewById(R.id.change_prompt_password);
        prompt_cpassword = view.findViewById(R.id.change_prompt_cpassword);
        button1 = view.findViewById(R.id.change_button1);

        b1 = view.findViewById(R.id.change_radioButton_male);
        b2 = view.findViewById(R.id.change_radioButton_female);
        if (Inf.stuInf != null) {
            if (Inf.stuInf.StudentSex) b1.setChecked(true);
            else b2.setChecked(true);
            sex = Inf.stuInf.StudentSex ? 1 : 0;
            music_text = Inf.stuInf.MusicPreference;
            music_preference.setText(Inf.stuInf.MusicPreference);
        }
        Spinner spinner = view.findViewById(R.id.change_spinner);
        ArrayAdapter<String> adapter = new ArrayAdapter<>(Inf.context, android.R.layout.simple_spinner_item, Inf.per_Array);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setSelection(0, false);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (position != 0) {
                    String selectedItem = parent.getItemAtPosition(position).toString();
                    if (music_preference.getText().toString().length() != 0)
                        music_preference.append("," + selectedItem);
                    else music_preference.append(selectedItem);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

        password.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                password_text = password.getText().toString().trim();
                if (!hasFocus && password_text.length() != 0) {
                    if (isValid(password_text, Inf.regex3)) {
                        prompt_password.setText("符合条件");
                        prompt_password.setTextColor(Color.GREEN);
                        password_st = true;
                    } else {
                        prompt_password.setText("输入有误");
                        password_st = false;
                    }
                } else if (hasFocus) {
                    prompt_password.setTextColor(Color.RED);
                    prompt_password.setText(R.string.prompt_detail3);
                    if (cpassword_st) {
                        prompt_cpassword.setTextColor(Color.RED);
                        prompt_cpassword.setText("两次密码不一致");
                        cpassword_st = false;
                    }
                }
            }
        });
        cpassword.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                cpassword_text = cpassword.getText().toString().trim();
                if (!hasFocus && cpassword_text.length() != 0) {
                    if (cpassword_text.equals(password_text)) {
                        prompt_cpassword.setTextColor(Color.GREEN);
                        prompt_cpassword.setText("符合条件");
                        cpassword_st = true;
                    } else {
                        prompt_cpassword.setTextColor(Color.RED);
                        prompt_cpassword.setText("两次密码不一致");
                        cpassword_st = false;
                    }
                }
            }
        });
        view.findViewById(R.id.change_button1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(old_password.length()!=0&&password_st&&cpassword_st){
                    new UpdatePasswordThread().start();
                }
            }
        });

        view.findViewById(R.id.change_del).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                music_text = music_preference.getText().toString();
                int last = music_text.lastIndexOf(',');
                if (last != -1)
                    music_preference.setText(music_text.substring(0, last));
                else music_preference.setText("");
            }
        });
        ((RadioGroup) view.findViewById(R.id.change_male_female)).setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId != -1) {
                    sex = checkedId == R.id.change_radioButton_male ? 1 : 0;
                } else {
                    sex = -1;
                }
            }
        });

        view.findViewById(R.id.change_button2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                music_text = music_preference.getText().toString();
                if (music_text.length() == 0) music_text = "无";
                new ChangeTread().start();
            }
        });
        return view;
    }

    class ChangeTread extends Thread {
        @Override
        public void run() {
            if (GetResult.changeDetailGR(Inf.stuInf.StudentAccount, music_text, sex) == 1) {
                Looper.prepare();
                Toast.makeText(Inf.context, "修改成功", Toast.LENGTH_SHORT).show();
                Inf.stuInf.StudentSex = sex == 1;
                Inf.stuInf.MusicPreference = music_text;
                Inf.centerFragment.music_preference.setText(Inf.stuInf.MusicPreference);
                Inf.centerFragment.sex.setImageResource(Inf.stuInf.StudentSex ? R.drawable.baseline_male_24 : R.drawable.baseline_female_24);
                Looper.loop();
            }
        }
    }
    class UpdatePasswordThread extends Thread{
        @Override
        public void run() {
            if(GetResult.updatePasswordGR(Inf.stuInf.StudentAccount,old_password.getText().toString(),cpassword_text)==1){
                Looper.prepare();
                Toast.makeText(Inf.context, "修改成功", Toast.LENGTH_SHORT).show();
                Looper.loop();
            }
            else {
                Looper.prepare();
                Toast.makeText(Inf.context, "旧密码错误", Toast.LENGTH_SHORT).show();
                Looper.loop();
            }
        }
    }


    private static boolean isValid(String text, String regex) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(text);
        return matcher.matches();
    }
}
